package com.example.presentshopping.ui.activity

import android.app.Dialog
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.TextView
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpActivity
import com.example.presentshopping.config.base.MyApplication
import com.example.presentshopping.config.bean.SignInBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.net.observer.TaskCallback
import com.example.presentshopping.ui.custom.NoDoubleClickListener
import com.example.presentshopping.ui.dialog.MessageDialog
import com.example.presentshopping.ui.fragment.frgmentdlog.SigninDialog
import com.example.presentshopping.utils.ToastUtil
import com.example.presentshopping.utils.glide.GlideUtil
import com.example.presentshopping.utils.tool.GsonUtils
import kotlinx.android.synthetic.main.activity_signin.*


@Suppress("DEPRECATION")
class SignInActivity : BaseMvpActivity() {

  var sindialog: SigninDialog ?=null
    var signBean: SignInBean? = null
    private var sendMesPresenter: PublicPresenter? = null

    var str = arrayOf(
        "1",
        "2",
        "3",
        "4"
    )
    override val layoutId: Int
        get() = R.layout.activity_signin

    override fun initView(savedInstanceState: Bundle?) {
        sindialog = SigninDialog()

        sendMesPresenter = PublicPresenter(context, false, "")
        sendMesPresenter?.attachView(this)

        GlideUtil.load(context, MyApplication.userInfoBean?.data?.avatar.toString(), myself_head)
        vs_myselft_account.text = MyApplication.userInfoBean?.data?.mobile
        fl_back.setOnClickListener { finish() }
        ts_test1.setFactory {
            val t = TextView(this@SignInActivity)
            t.gravity = Gravity.CENTER
            t.setTextAppearance(this@SignInActivity, R.style.BlueText)
            t
        }
        ts_test2.setFactory {
            val t = TextView(this@SignInActivity)
            t.gravity = Gravity.CENTER
            t.setTextAppearance(this@SignInActivity, R.style.BlueText)
            t
        }
        ts_test3.setFactory {
            val t = TextView(this@SignInActivity)
            t.gravity = Gravity.CENTER
            t.setTextAppearance(this@SignInActivity, R.style.BlueText)
            t
        }


        lsiane_sbu.setOnClickListener(onClickListener)

    }

    private var onClickListener: View.OnClickListener = object : NoDoubleClickListener() {
        @Throws(Exception::class)
        override fun onNoDoubleClick(v: View) {
            userSign()
        }
    }


    override fun onResume() {
        sendMesPresenter?.netWorkRequestGet(HttpURL.Req_signInfo)
        super.onResume()
    }

    fun userSign() {
        sendMesPresenter?.netWorkRequestGet(HttpURL.Req_userSign, object : TaskCallback {
            override fun onSuccess(data: String?) {
                onResume()
                MessageDiala()
            }

            override fun onFailure(
                e: Throwable?,
                code: Int,
                msg: String?,
                isNetWorkError: Boolean
            ) {
                ToastUtil.showMsg(msg)
            }

        })
    }

    fun MessageDiala() {
        sindialog?.show(supportFragmentManager,"")

    }


    override fun onDestroy() {
        sendMesPresenter?.detachView()
        super.onDestroy()
    }

    override fun showToast(msg: String?) {
        ToastUtil.showMsg(msg)
    }

    override fun onSuccess(data: String?) {
        signBean = GsonUtils.getGson().fromJson(data, SignInBean().javaClass)
        //  tv_integral?.text = "已连续签到： $time 天"
        signBean?.data?.time?.toInt()?.let { skyname.setanInt(it) }
        if (signBean?.data?.status==1){
            lsiane_sbu.text ="已签到"
        }
        val str =   signBean?.data?.draw.toString().toCharArray()

        when(str.size){
            1->{
                ts_test1.setText("0")
                ts_test2.setText("0")
                ts_test3.setText(str[0].toString())
            }
            2->{
                ts_test1.setText("0")
                ts_test2.setText(str[0].toString())
                ts_test3.setText(str[1].toString())
            }
            3->{
                ts_test1.setText(str[0].toString())
                ts_test2.setText(str[1].toString())
                ts_test3.setText(str[2].toString())
            }
            else->{
                ts_test1.setText(str[0].toString())
                ts_test2.setText(str[1].toString())
                ts_test3.setText(str[2].toString())
            }
        }
    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }
}